package database;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;

import object.Department;
import utility.StringUtil;
import clientMain.Login;

public class DepartmentDelegate {
	
	public int checkDepName(String depName, int depCom){
		int check = -1;
		try {
			Login.conn.setAutoCommit(false);
			CallableStatement cst = Login.conn.prepareCall("{call sp_CheckDepName(?,?,?)}");
			cst.setString(1, StringUtil.setFilterIllegal(depName));
			cst.setInt(2, depCom);
			cst.registerOutParameter(3, Types.INTEGER);
			cst.executeUpdate();
			Login.conn.commit();
			check = cst.getInt(3);
			return check;
		} catch (Exception e) {
			return check;
		}
	}
	
	public boolean insertDepartment(Department dep, int userInsert){
		boolean flag = true;
		try {
			Login.conn.setAutoCommit(false);
			CallableStatement cst = Login.conn.prepareCall("{call sp_InsertDep(?,?,?,?,?,?,?,?)}");
			cst.setString(1, StringUtil.setFilterIllegal(dep.getDepName()));
			cst.setString(2, StringUtil.setFilterIllegal(dep.getDepDescribe()));
			cst.setInt(3, dep.getDepTotalEm());
			cst.setString(4, StringUtil.setFilterIllegal(dep.getDepAddress()));
			cst.setInt(5, dep.getDepCurrentCity());
			cst.setString(6, StringUtil.setFilterIllegal(dep.getDepPhone()));
			cst.setInt(7, userInsert);
			cst.setInt(8, dep.getDepCompany());
			cst.executeUpdate();
			Login.conn.commit();
		} catch (Exception e) {
			flag = false;
		}
		return flag;
	}
	
	public boolean updateDepartment(Department dep, int userInsert){
		boolean flag = true;
		try {
			Login.conn.setAutoCommit(false);
			CallableStatement cst = Login.conn.prepareCall("{call sp_UpdateDep(?,?,?,?,?,?,?,?)}");
			cst.setInt(1, dep.getDepId());
			cst.setString(2, StringUtil.setFilterIllegal(dep.getDepName()));
			cst.setString(3, StringUtil.setFilterIllegal(dep.getDepDescribe()));
			cst.setString(4, StringUtil.setFilterIllegal(dep.getDepAddress()));
			cst.setInt(5, dep.getDepCurrentCity());
			cst.setString(6, StringUtil.setFilterIllegal(dep.getDepPhone()));
			cst.setInt(7, userInsert);
			cst.setInt(8, dep.getDepCompany());
			cst.executeUpdate();
			Login.conn.commit();
		} catch (Exception e) {
			flag = false;
		}
		return flag;
	}
	public Department getDep(int depId){
		Department dep = new Department();
		try {
			Login.conn.setAutoCommit(false);
			CallableStatement cst = Login.conn.prepareCall(" {call sp_getDepartment(?)} ");
			cst.setInt(1, depId);
			ResultSet rs = cst.executeQuery();
			while(rs.next()){
				dep.setDepId(depId);
				dep.setDepName(StringUtil.getFilterIllegal(rs.getString("Dep_Name")));
				dep.setDepDescribe(StringUtil.getFilterIllegal(rs.getString("Dep_Describe")));
				dep.setDepTotalEm(rs.getInt("Dep_TotalEm"));
				dep.setDepPhone(StringUtil.getFilterIllegal(rs.getString("Dep_Phone")));
				dep.setDepAddress(StringUtil.getFilterIllegal(rs.getString("Dep_Address")));
				dep.setDepCurrentCity(rs.getInt("Dep_CurrentCity"));
//						StringUtil.getFilterIllegal(rs.getString("DistrictName")),
//						StringUtil.getFilterIllegal(rs.getString("ProvinceName")));
			}
		} catch (SQLException e) {
			e.printStackTrace();
			dep = null;
		}
		return dep;
	}
}
